def subsets(nums):
    """
    :type nums: List[int]
    :rtype: List[List[int]]
    """
    res = []
    res.append([])
    # 按照子集的元素个数分类寻找
    for i in range(1, len(nums) + 1):
        backtracking(nums, res, i, 0, [])
    return res


def backtracking(nums, res, length, index, subset):
    # 当找到的结果满足，插入到结果集中
    if len(subset) == length:
        temp = subset.copy()
        res.append(temp)
        return
    # 查找过程
    for i in range(index, len(nums)):
        subset.append(nums[i])
        # 递归当前元素的后一个元素
        backtracking(nums, res, length, i + 1, subset)
        subset.pop()


nums = [1, 2, 3]
print(subsets(nums))
# [[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]